<template>
	<view>
		<!-- 搜索框组件 -->
		<view class="search-box">
			<mySearch @click1="click1"></mySearch>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<!-- 页面跳转 -->
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类区域 -->
		<view class="nav-list">
			<view v-for="(item, index) in navList" :key="index" @click="navClick(item)">
				<image class="nav-img" :src="item.image_src" mode=""></image>
			</view>
		</view>
		<!-- 楼层区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 标题区域 -->
				<image class="floor-title-img" :src="item.floor_title.image_src" mode=""></image>
				<!-- 图片区域 -->
				<view class="floor-img-box">
					<!-- 左边区域 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :style="{width: item.product_list[0].image_width + 'rpx'}"
							:src="item.product_list[0].image_src" mode="widthFix"></image>
					</navigator>
					<!-- 右边区域 -->
					<view class="right-img-box">
						<navigator v-for="(item1, index1) in item.product_list" :key="index1" v-if="index1 !== 0"
							:url="item1.url">
							<image :style="{width: item1.image_width + 'rpx'}" :src="item1.image_src" mode="widthFix">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mySearch from '@/components/my-search/my-search.vue'
	export default {
		components: {
			mySearch
		},
		data() {
			return {
				// 轮播图列表
				swiperList: [],
				// 分类列表
				navList: [],
				// 楼层列表
				floorList: []
			};
		},
		onLoad() {
			// 获取轮播图列表
			this.getSwiperList();
			// 获取分类列表
			this.getNavList();
			// 获取楼层列表
			this.getFloorList();
		},
		methods: {
			// 获取轮播图列表
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata');
				if (res.meta.status !== 200) return uni.$showMsg();
				this.swiperList = res.message;
			},
			// 获取分类列表
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems');
				if (res.meta.status !== 200) return uni.$showMsg();
				this.navList = res.message;
			},
			// 分类跳转
			navClick(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/index'
					})
				}
			},
			// 获取楼层列表
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg();
				// 循环替换
				res.message.forEach(floor => {
					floor.product_list.forEach(product => {
						product.url = "/subpkg/goods_list/goods_list?" + product.navigator_url.split(
							'?')[1];
					})
				})
				this.floorList = res.message;
			},
			click1() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 搜索框
	.search-box {
		// 设置粘性定位、吸顶效果
		position: sticky;
		// 吸顶的位置
		top: 0;
		// 提高层级、反之被覆盖
		z-index: 999;
	}

	// 轮播图
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	//分类
	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15rpx 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	// 楼层标题
	.floor-item {
		width: 100%;

		.floor-title-img {
			margin-top: 20rpx;
			width: 300px;
			height: 80rpx;
		}
	}

	// 楼层图片
	.floor-img-box {
		display: flex;

		.left-img-box {
			padding-left: 10rpx;
		}

		.right-img-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}
</style>